package com.xiayuanxing.arithmetic.DataStructures.Sort;

import java.util.Arrays;

/**
 * @program: arithmetic
 * @description: 选择排序
 * @author: xia yuan xing
 * @create: 2021-09-23 18:11
 */
public class SelectionSort {

    public static void main(String[] args) {

        int[] array = {-1, 6, 3, 12, 80, -9, -5, -93, 120};
        selectSort(array);

    }


    /**
     * 选择排序 时间复杂度 O(n^2)
     *
     * @param array
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int minIndex = i;
            int min = array[i];
            for (int j = i + 1; j < array.length; j++) {
                if (min > array[j]) {
                    min = array[j];
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                array[minIndex] = array[i];
                array[i] = min;
            }
        }

        System.out.println(Arrays.toString(array));
    }

}